﻿using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace CornerMemo
{
    /// <summary>
    /// 键盘输入
    /// </summary>
    public class KeyboardInput : MonoBehaviour
    {
        /// <summary>
        /// 需要激活事件的按键列表
        /// </summary>
        public List<KeyCode> keyCodes = new List<KeyCode>();
        
        private void Update()
        {
            foreach (var keyCode in keyCodes)
            {
                if (Input.GetKeyDown(keyCode))
                {
                    EventCenter.Fire(this, KeyboardDownEventArgs.Create(keyCode));
                }
                else if (Input.GetKeyUp(keyCode))
                {
                    EventCenter.Fire(this, KeyboardUpEventArgs.Create(keyCode));
                }
            }
        }

        private void FixedUpdate()
        {
            foreach (var keyCode in keyCodes)
            {
                if (Input.GetKey(keyCode))
                {
                    EventCenter.Fire(this, KeyboardHoldEventArgs.Create(keyCode));
                }
            }
        }
    }
}